from AKapi.settings.const import BANNER_COUNTER
from .celery import app
from django.core.cache import cache
from home import models
from home import serializer


@app.task
def banner_update():

    """
    定时更新首页轮播图
    :return:
    """

    queryset_banner = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('orders')[:BANNER_COUNTER]

    serializer_banner = serializer.BannerModelSerializer(instance=queryset_banner, many=True)
    print(serializer_banner.data)
    # banner_list = cache.get('banner_list')
    # print(banner_list)
    for banner in serializer_banner.data:
        banner['img'] = 'http://127.0.0.1:8000' + banner['img']
    cache.set('banner_list', serializer_banner.data)
    banner_list = cache.get('banner_list')
    print(banner_list)
    return True